package com.langshixiaobai.mqdemo.server.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

//队列
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MSGQueue {
    //唯一标识
    private String name;
    //是否开启持久化，false否，true是
    private Boolean durable ;
    //是否独占
    private Boolean exclusive ;
    //自动删除
    private Boolean autoDelete ;
    //参数
    private Map<String, Object> arguments = new HashMap<>();

    //转换
    public String getArguments() {
        //用于转换和解析json字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String s ;
        try {
            s = objectMapper.writeValueAsString(arguments);
            return s;
        } catch (JsonProcessingException e) {
            System.out.println("转换Json错误");
        }
        return "{}";

    }
    //解析
    public void setArguments(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //Json解析成HashMap类型
            arguments = objectMapper.readValue(json, new TypeReference<HashMap<String, Object>>(){
            });
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public void setArguments(String key, Object value){
        arguments.put(key,value);
    }

    public Object getArguments(String key){
        return arguments.get(key);
    }

}
